// ==UserScript==
// @name Timer
// @namespace https://gitee.com/Atomicyo_chen_wei_zuan
// @version 0.1
// @description Refresh the page automatically for timing.
// @author Atomicyo
// @match https://www.baidu.com/*
// @include *
// @grant none
// ==/UserScript==
(function() {
    'use strict';
    function createScriptElement() {
        var divElm = document.createElement("div");
        divElm.style.cssText = "position: fixed;top: 8em; right: 8em; z-index:100000; height:100px; width:100px;";
        divElm.id = "timer";
        var hourS = document.createElement("strong");
        var hourText = document.createTextNode("00:");
        hourS.id = "hour";
        hourS.style.fontSize = "40px";
        hourS.appendChild(hourText);
        divElm.appendChild(hourS);
        var minuteS = document.createElement("strong");
        var minuteText = document.createTextNode("00:");
        minuteS.id = "minute";
        minuteS.style.fontSize = "40px";
        minuteS.appendChild(minuteText);
        divElm.appendChild(minuteS);
        var secondS = document.createElement("strong");
        var secondText = document.createTextNode("00");
        secondS.id = "second";
        secondS.style.fontSize = "40px";
        secondS.appendChild(secondText);
        divElm.appendChild(secondS);
        return divElm;
    }
    function createGoogleScripts(element){
        document.body.appendChild(element);
        console.log('replaced google jquery with '+document.title);
    }
    function timer(intDiff, element) {
        window.setInterval(function (){
            var hour = 0,
                minute = 0,
                second = 0;//时间默认值
            hour = Math.floor(intDiff / 3600);
            minute = Math.floor(intDiff / 60) - (minute * 60);
            second = Math.floor(intDiff) - (minute * 60);
            var timerList = document.querySelectorAll('strong');
            timerList.forEach(function(timerOld) {
                if (timerOld.id == "hour") {
                    if (hour <=9) hour = '0' +hour;
                    var hourS = document.createElement("strong");
                    var hourText = document.createTextNode(hour+":");
                    hourS.id = "hour";
                    hourS.style.fontSize = "40px";
                    hourS.appendChild(hourText);
                    timerOld.parentNode.replaceChild(hourS, timerOld);
                };
                if (timerOld.id == "minute") {
                    if (minute <= 9) minute = '0' + minute;
                    var minuteS = document.createElement("strong");
                    var minuteText = document.createTextNode(minute+":");
                    minuteS.id = "minutes";
                    minuteS.style.fontSize = "40px";
                    minuteS.appendChild(minuteText);
                    timerOld.parentNode.replaceChild(minuteS, timerOld);
                };
                if (timerOld.id == "second") {
                    if (second <= 9) second = '0' + second;
                    var secondS = document.createElement("strong");
                    var secondText = document.createTextNode(second);
                    secondS.id = "second";
                    secondS.style.fontSize = "40px";
                    secondS.appendChild(secondText);
                    timerOld.parentNode.replaceChild(secondS, timerOld);
                };
            });
            intDiff++;
        }, 1000);
    }
    var element = createScriptElement();
    createGoogleScripts(element);
    var intDiff = parseInt(0);//初始化时间
    timer(intDiff, element);
})();